package com.unionx.unionxuf.service;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.unionx.unionxuf.common.DynamicConnect;
import com.unionx.unionxuf.common.ShuZuUtil;
import com.unionx.unionxuf.common.TransformerStr;
import com.unionx.unionxuf.entity.BillMain;
import com.unionx.unionxuf.entity.BillMainPub;
import com.unionx.unionxuf.entity.BillSub;
import com.unionx.unionxuf.entity.Cobp;
import com.unionx.unionxuf.entity.ComBill;
import com.unionx.unionxuf.entity.ComBill2;
import com.unionx.unionxuf.entity.CropDTD;
import com.unionx.unionxuf.entity.Excel;
import com.unionx.unionxuf.entity.MergPram;
import com.unionx.unionxuf.entity.ReceivepayManger;
import com.unionx.unionxuf.entity.Record;
import com.unionx.unionxuf.entity.SettleDTD;
import com.unionx.unionxuf.entity.Settlestyle;
import com.unionx.unionxuf.entity.Settlestylecopy;
import com.unionx.unionxuf.entity.Ysyf;
import com.unionx.unionxuf.localdao.BillMainMapper;
import com.unionx.unionxuf.localdao.BillSubMapper;
import com.unionx.unionxuf.localdao.ComBillMapper;
import com.unionx.unionxuf.localdao.ReceivepayMangerMapper;
import com.unionx.unionxuf.localdao.SettlestyleMapper;

@Service("recordService")
public class RecordServiceImpl implements RecordService {
	private Statement stmt = null;
	private Connection conn = null;
	@Autowired
	private BillMainService bs;

	@Autowired(required = true)
	private BillMainMapper billMainMapper;

	@Autowired
	private BillSubMapper billSubMapper;

	@Autowired
	private SettlestyleMapper sm;

	@Autowired
	private ReceivepayMangerMapper rmm;

	@Autowired
	private ComBillMapper cbm;
    
	@Autowired
	private SqlSessionFactoryBean sqlSessionFactory;
	
public Object saveRecord(HttpServletRequest request,Record r) {
		
		Map<String,Object> map = new HashMap<String,Object>(); 
		HttpSession session=request.getSession();
		String logindate=(String) session.getAttribute("logindate");
		System.out.println(logindate);	
		String[] str=logindate.split("-");
		String iyear=str[0];
		String iperiod=str[1];
		int index=r.getIndex();
		int size=r.getSize();
		 int m=0;
		 int inid=1;
		String pd = iperiod.replaceFirst("^0*", "");  
		System.out.println(pd+","+iyear);
		conn= (Connection) DynamicConnect.getConnectionInfo(request);
	    String sql = "select bflag from gl_mend  where iperiod='"+pd+"' and iyear='"+iyear+"'";
	    String pzh = "select  max(ino_id) from gl_accvouch" +" where iyear="+ "'"+iyear+"'"+" and  iperiod="+"'"+iperiod+"'";
		
	    try {
	    	int cc=0;
	    	int ct=0;
	    	stmt = conn.createStatement();
			ResultSet i =stmt.executeQuery(sql);
			while(i.next()){
				cc=Integer.parseInt(i.getString(1));
			}
			if(cc==0){
				    String lsh=r.getSerialno();
				    String cid=r.getCropid();
				    String sz=r.getZhuangtai();
				    String idoc="-1";
				    String cbill=(String) session.getAttribute("name");
				    String  csign = "记";//凭证类别
				    String  isignseq ="1"; //凭证类别排序号
				    
				    ResultSet j= stmt.executeQuery(pzh);
				    while(j.next()){
						
				    	if(j.getString(1)==null){
				    		ct=1;
				    		
				    		if(ct<size){
				    		      m=1;	
				    		}
				    	}else{
				    		
				    		 ct=Integer.parseInt(j.getString(1));
				    		 if(ct+1<size){
				    			 m=ct;
				    		 }
				    	}
				    	
					}
				   
				   // String ino_id="10";
				   
				   // String dbill_date=new Date().toString(); //
				    
				    String cdigest= r.getRemark();
				    String ccode=r.getKmname();
				    String mc=r.getMcf();//st.get(0).getMcf();
				    String md=r.getMdf();
				    String iYperiod=iyear+iperiod;
				  
							    String s = "insert into gl_accvouch (iperiod,csign ,isignseq,ino_id,inid,dbill_date,idoc,cbill,cdigest,ccode,md,mc,iyear,iYPeriod)"
							 		+ "values('"+iperiod+"','"+csign+"','"+isignseq+"','"+inid+"','"+m+"','"+logindate+"','"+idoc+"','"+cbill+"','"+cdigest+"','"+ccode+"','"+md+"','"+mc+"','"+iyear+"','"+iYperiod+"')";
							   
							    stmt = conn.createStatement();
							  
							    
							    int x= stmt.executeUpdate(s);
							   
							    if(x!=0){
							    	   
							    	   bs.updateCtype(request, r.getCropid(), r.getSerialno(), "1");
							    	   map.put("status", 0);
							    	   return map;
							    }else{
							    	  map.put("status", 1);
							    	  return map;
							    }
					
					 }
			
		}
		  catch (SQLException e) {
			  map.put("status", 1);
			  return map;
			
			
		}
		return map; 

		
	}


@Override
public Object saveRecords(HttpServletRequest request, String cropid, String serialno, String zhuangtai, String g1,
		String g2, String g3, String g4,String g5) {
	// TODO Auto-generated method stub
	
	
	 String dai[] =g3.split(",");
	 List<Float> list = new ArrayList<Float>();
	 for(int l=0;l<dai.length;l++){
		 if(dai[l]==null || dai[l].equals("null")){
			 dai[l]="0";
			 list.add(Float.parseFloat(dai[l]));
		 }else{
			 list.add(Float.parseFloat(dai[l]));
		 }
	 }
	 
	 
	 
	 String jie[] =g4.split(",");
	 List<Float> list2 = new ArrayList<Float>();
	 for(int l=0;l<jie.length;l++){
		 if(jie[l]==null || jie[l].equals("null")){
			 jie[l]="0";
			 list2.add(Float.parseFloat(jie[l]));
		 }else{
			 list2.add(Float.parseFloat(jie[l]));
		 }
	 }
	 
	
	Map<String,Object> map = new HashMap<String,Object>(); 
	HttpSession session=request.getSession();
	String logindate=(String) session.getAttribute("logindate");
	System.out.println(logindate);	
	String[] str=logindate.split("-");
	String iyear=str[0];
	String iperiod=str[1];
	String pd = iperiod.replaceFirst("^0*", "");  
	System.out.println(pd+","+iyear);
	conn= (Connection) DynamicConnect.getConnectionInfo(request);
    String sql = "select bflag from gl_mend  where iperiod='"+pd+"' and iyear='"+iyear+"'";
    String pzh = "select  max(ino_id) from gl_accvouch" +" where iyear="+ "'"+iyear+"'"+" and  iperiod="+"'"+iperiod+"'";
	
    int  x=0;
	try {
		
		int cc=0;
    	int ct=0;
    	stmt = conn.createStatement();
		ResultSet i =stmt.executeQuery(sql);
		while(i.next()){
			cc=Integer.parseInt(i.getString(1));
		}
		if(cc==0){	
			 	String lsh=serialno;
			    String cid=cropid;
			    String sz=zhuangtai;
			    String idoc="-1";
			    String cbill=(String) session.getAttribute("name");
			    String  csign = "记";//凭证类别
			    String  isignseq ="1"; //凭证类别排序号
			    String iYperiod=iyear+iperiod;
			    
			    
			    int m=0;
			    ResultSet j= stmt.executeQuery(pzh);
			    while(j.next()){
					
			    	if(j.getString(1)==null){
			    		m=1;
			    	}else{  		
			    	m=ct=Integer.parseInt(j.getString(1))+1;	
			    	}
			    	
				}
			    
			    String str1[] =g1.split(",");
			    String str2[] =g2.split(",");
			    String str5[]=g5.split(",");
			    
			    if(str1.length>1){
			    	
			    	for(int c=0;c<str1.length;c++){
			    		
			   String s = "insert into gl_accvouch (iperiod,csign ,isignseq,ino_id,inid,dbill_date,idoc,cbill,cdigest,ccode,md,mc,iyear,iYPeriod)"
					+ "values('"+iperiod+"','"+csign+"','"+isignseq+"','"+m+"','"+(c+1)+"','"+logindate+"','"+idoc+"','"+cbill+"','"+str1[c]+"','"+str5[c]+"','"+list.get(c)+"','"+list2.get(c)+"','"+iyear+"','"+iYperiod+"')";			    		
			    		
			   stmt = conn.createStatement();
		    
			  x= stmt.executeUpdate(s);
			    	
			   
			 }
			        if(x!=0){
				    	   
				    	   bs.updateCtype(request, cropid,serialno, "1");
				    	   map.put("status", 0);
				    	  
				    }else{
				    	  map.put("status", 1);
				    	  return map;
				    	}
				   
		  }
		
		}
		
		
	} catch (Exception e) {
		// TODO: handle exception
		e.printStackTrace();
		  map.put("status", 1);
		  return map;
		
	}
	
	return map;
}

public Object getCB(HttpServletRequest request, ComBill2 cbs) {
    
	Map map = new HashMap();
    Map map1 = new HashMap();

    List listbs = this.billSubMapper.getAllBillSub(cbs.getCropid());
    
    List listlsh = new ArrayList();
    List listssc = new ArrayList();
    List listbmp = new ArrayList();
    List ylist = new ArrayList();
    List<String>  listsst=  new ArrayList<String>(); 
    List<ComBill> listcb = new ArrayList<ComBill>();

    for (int i = 0; i < listbs.size(); i++) {
      listlsh.add(((BillSub)listbs.get(i)).getSerialno());
    }

    for (int i = 0; i < listbs.size(); i++) {
      List pa = new ArrayList();
      Float mc = Float.valueOf(0.0F);
      Float md = Float.valueOf(0.0F);
      Float fpa = Float.valueOf(0.0F);

       CropDTD ccc = new CropDTD();
      ccc.setCorpid(cbs.getCropid());
      ccc.setIsno((String)listlsh.get(i));
      List bslsh = this.billSubMapper.getProbyLsandCid(ccc);

      for (int y = 0; y < bslsh.size(); y++)
      {
        String spa = ((BillSub)bslsh.get(y)).getPriceAfterDiscount();
        pa.add(spa);
      }
      for (int y = 0; y < pa.size(); y++) {
        fpa = Float.valueOf(fpa.floatValue() + Float.parseFloat((String)pa.get(y)));
      }
      CropDTD cc = new CropDTD();
      cc.setCorpid(cbs.getCropid());
      cc.setIsno((String)listlsh.get(i));
      List listss = this.sm.getSS(cbs.getCropid(), (String)listlsh.get(i));
      
      
      List<SettleDTD> listjiesuanbz = this.sm.getJiesuanbz(cbs.getCropid(),(String)listlsh.get(i));

    	  
    	  StringBuffer sb=new StringBuffer();
    	  for(SettleDTD item:listjiesuanbz){
    	  sb.append(item.toString());
    	  }
    	  listsst.add(sb.toString());
      
      List listrmmc = this.rmm.getRM(cbs.getCropid(), (String)listlsh.get(i), "1");
      List mcs = new ArrayList();
      List listrmmd = this.rmm.getRM(cbs.getCropid(), (String)listlsh.get(i), "0");
      List mds = new ArrayList();
      Settlestylecopy ss = new Settlestylecopy();
      float sum1 =0;
      float sum2 =0;
      float sum3 =0;
      float sum4 =0;
      float sum5 =0;
      float sum6 =0;
      float sum7 =0;
      float sum8 =0;
      float sum9 =0;
      float sum10 =0;
      float sum11 =0;
      float sum12 =0;
      float sum13 =0;
      float sum14 =0;
      float sum15 =0;
      float sum16 =0;
      float sum17 =0;
      float sum18 =0;
      String mxdtd="";
      String stbzdtd="";
      
      BillMainPub bmp = new BillMainPub();
      CropDTD c = new CropDTD();
      c.setCorpid(cbs.getCropid());
      c.setIsno((String)listlsh.get(i));
      List bmls = this.billMainMapper.getProbyLsandCids(c);
             for (int j = 0; j < listss.size(); j++)
      {
        if (((Settlestyle)listss.get(j)).getSettlement().equals("现金")) {
          ss.setCash("现金");
          sum1+=Float.parseFloat(((Settlestyle)listss.get(j)).getAmount());
          ss.setCashe(sum1+"");
        }
        if (((Settlestyle)listss.get(j)).getSettlement().equals("刷卡POS")) {
          ss.setShuaka("刷卡POS");
          sum2+=Float.parseFloat(((Settlestyle)listss.get(j)).getAmount());
          ss.setShuakae(sum2+"");
        }
        if (((Settlestyle)listss.get(j)).getSettlement().equals("支票")) {
          ss.setZhipiao("支票");
          sum3+=Float.parseFloat(((Settlestyle)listss.get(j)).getAmount());
          ss.setZhipiaoe(sum3+"");
        }
        if (((Settlestyle)listss.get(j)).getSettlement().equals("电汇")) {
          ss.setDianhui("电汇");
          sum4+=Float.parseFloat(((Settlestyle)listss.get(j)).getAmount());
          ss.setDianhuie(sum4+"");
        }
        if (((Settlestyle)listss.get(j)).getSettlement().equals("欠款")) {
          ss.setQiankuan("欠款");
          sum5+=Float.parseFloat(((Settlestyle)listss.get(j)).getAmount());
          ss.setQiankuane(sum5+"");
        }
        if (((Settlestyle)listss.get(j)).getSettlement().equals("划卡")) {
          ss.setHuaka("划卡");      
          sum6+=Float.parseFloat(((Settlestyle)listss.get(j)).getAmount());
          ss.setHuakae(sum6+"");
        }
        if (((Settlestyle)listss.get(j)).getSettlement().equals("押金转入")) {
          ss.setYajintrans("押金转入");
          sum7+=Float.parseFloat(((Settlestyle)listss.get(j)).getAmount());
          ss.setYajintranse(sum7+"");
        }

        if (((Settlestyle)listss.get(j)).getSettlement().equals("网站收入A")) {
          ss.setSitea("网站收入A");
          ss.setSiteae(((Settlestyle)listss.get(j)).getAmount());
        }
        if (((Settlestyle)listss.get(j)).getSettlement().equals("网站收入B")) {
          ss.setSiteb("网站收入B");
          ss.setSitebe(((Settlestyle)listss.get(j)).getAmount());
        }
        if (((Settlestyle)listss.get(j)).getSettlement().equals("福卡")) {
          ss.setFuka("福卡");
          sum8+=Float.parseFloat(((Settlestyle)listss.get(j)).getAmount());
          ss.setFukae(sum8+"");
        }

        if (((Settlestyle)listss.get(j)).getSettlement().equals("服务项目")) {
          ss.setBeiyong("服务项目");
          sum9+=Float.parseFloat(((Settlestyle)listss.get(j)).getAmount());
          ss.setBeiyonge(sum9+"");
        }

        if (((Settlestyle)listss.get(j)).getSettlement().equals("美尔贝")) {
          ss.setMeibeier("美尔贝");
          sum10+=Float.parseFloat(((Settlestyle)listss.get(j)).getAmount());
          ss.setMeibeiere(sum10+"");
        }

        if (((Settlestyle)listss.get(j)).getSettlement().equals("诺美尔")) {
          ss.setNuomeier("诺美尔");
          sum11+=Float.parseFloat(((Settlestyle)listss.get(j)).getAmount());
          ss.setNuomeiere(sum11+"");
        }

        if (((Settlestyle)listss.get(j)).getSettlement().equals("依美网 ")) {
          ss.setYimei("依美网 ");
          sum12+=Float.parseFloat(((Settlestyle)listss.get(j)).getAmount());
          ss.setYimeie(sum12+"");
        }

        if (((Settlestyle)listss.get(j)).getSettlement().equals("东方虹")) {
          ss.setGong("东方虹");
          sum13+=Float.parseFloat(((Settlestyle)listss.get(j)).getAmount());
          ss.setGonge(sum13+"");
        }

        if (((Settlestyle)listss.get(j)).getSettlement().equals("爱立邦")) {
          ss.setAilibang("爱立邦");
          sum14+=Float.parseFloat(((Settlestyle)listss.get(j)).getAmount());
          ss.setAilibange(sum14+"");
        }

        if (((Settlestyle)listss.get(j)).getSettlement().equals("更美")) {
          ss.setGengmei("更美");
          sum15+=Float.parseFloat(((Settlestyle)listss.get(j)).getAmount());
          ss.setGengmeie(sum15+"");
        }

        if (((Settlestyle)listss.get(j)).getSettlement().equals("新氧")) {
          ss.setXinyang("新氧");
          sum16+=Float.parseFloat(((Settlestyle)listss.get(j)).getAmount());
          ss.setXinyange(sum16+"");
        }

        if (((Settlestyle)listss.get(j)).getSettlement().equals("悦美")) {
          ss.setYuemei("悦美");
          sum17+=Float.parseFloat(((Settlestyle)listss.get(j)).getAmount());
          ss.setYuemeie(sum17+"");
        }

        if (((Settlestyle)listss.get(j)).getSettlement().equals("真优美")) {
          ss.setZhenyoumei("真优美");
          sum18+=Float.parseFloat(((Settlestyle)listss.get(j)).getAmount());
          ss.setZhenyoumeie(sum18+"");
        }
        
        String mx = null;
        String bz =null;
        if(((Settlestyle)listss.get(j)).getMingxi().equals("")){
        		mx="无";
        }else{
        		mx=((Settlestyle)listss.get(j)).getMingxi();
        }

        if(((Settlestyle)listss.get(j)).getTips().equals("")){
        	bz="无";
        }else{
        	bz=((Settlestyle)listss.get(j)).getTips();
        }
        mxdtd+=mxdtd==""?mx:"||"+mx;
        ss.setMingxi(mxdtd);
        stbzdtd+=stbzdtd==""?bz:"||"+bz;
        ss.setStbeizhu(stbzdtd);
      }

      for (int x = 0; x < bmls.size(); x++) {
        bmp.setCusname(((BillMain)bmls.get(x)).getCustomer());
        bmp.setBankadate(((BillMain)bmls.get(x)).getCardtime());
        bmp.setYueyedate(((BillMain)bmls.get(x)).getReservatime());
        bmp.setDate(((BillMain)bmls.get(x)).getDate());
        bmp.setFapiaohao(((BillMain)bmls.get(x)).getInvoiceno());
        bmp.setChukudan(((BillMain)bmls.get(x)).getOutboundorder());
        bmp.setZkl(((BillMain)bmls.get(x)).getDiscount());
        bmp.setYhje(((BillMain)bmls.get(x)).getBillmaincol3());
        bmp.setShoukuanren(((BillMain)bmls.get(x)).getShoukuanren());
      }

      Ysyf y = new Ysyf();
      if ((listrmmc.size() != 0) || (listrmmc != null)) {
        for (int m = 0; m < listrmmc.size(); m++) {
          mcs.add(((ReceivepayManger)listrmmc.get(m)).getAmount());
          mc = Float.valueOf(mc.floatValue() + Float.parseFloat((String)mcs.get(m)));
          y.setYscrop(((ReceivepayManger)listrmmc.get(m)).getCorp());
        }
      }
      if ((listrmmc.size() == 0) || (listrmmc == null)) {
        y.setYscrop("");
      }
      if ((listrmmd.size() != 0) || (listrmmd != null)) {
        for (int m = 0; m < listrmmd.size(); m++) {
          mds.add(((ReceivepayManger)listrmmd.get(m)).getAmount());
          md = Float.valueOf(md.floatValue() + Float.parseFloat((String)mds.get(m)));
          y.setYfcrop(((ReceivepayManger)listrmmd.get(m)).getCorp());
        }
      }
      if ((listrmmd.size() == 0) || (listrmmd == null)) {
        y.setYfcrop("");
      }

      ylist.add(y);
      listssc.add(ss);
      listbmp.add(bmp);
      ComBill cb = new ComBill();
      cb.setDate(((BillMainPub)listbmp.get(i)).getDate());	
      cb.setShoushudoc(((BillSub)listbs.get(i)).getOperationDoctor());
      cb.setPeitai(((BillSub)listbs.get(i)).getSecondDoctor());
      cb.setFeishoushu(((BillSub)listbs.get(i)).getUnoperationDoctor());
      cb.setYsassit(((BillSub)listbs.get(i)).getAssistantDictor());
      cb.setCusname(((BillMainPub)listbmp.get(i)).getCusname());
      cb.setXfdl(((BillSub)listbs.get(i)).getConsumeclass());
      cb.setXmfl(((BillSub)listbs.get(i)).getProject());
      cb.setXfmx(((BillSub)listbs.get(i)).getSubinfo());
      cb.setJfje(((BillSub)listbs.get(i)).getPriceAfterDiscount());
      cb.setFssbz(((BillSub)listbs.get(i)).getFssbz());
      cb.setZhekopulv(((BillSub)listbs.get(i)).getDiscount());
      cb.setSjje(((BillSub)listbs.get(i)).getSjje());
      cb.setXczx(((BillSub)listbs.get(i)).getXczx());
      cb.setPhonezx(((BillSub)listbs.get(i)).getPhonezx());
      cb.setWebzx(((BillSub)listbs.get(i)).getWebzx());
      cb.setScb(((BillSub)listbs.get(i)).getScb());
      cb.setMenzhen(((BillSub)listbs.get(i)).getMenzhen());
      cb.setQita(((BillSub)listbs.get(i)).getOtherpp());
      cb.setMembername(((BillSub)listbs.get(i)).getHydw());
  	  cb.setSitea(((Settlestylecopy) listssc.get(i)).getSiteae());
  	  cb.setSiteb(((Settlestylecopy) listssc.get(i)).getSitebe());
      cb.setYouhuijine(((BillSub)listbs.get(i)).getAmount());
      cb.setCash(((Settlestylecopy)listssc.get(i)).getCashe());
      cb.setShuaka(((Settlestylecopy)listssc.get(i)).getShuakae());
      cb.setZhipiao(((Settlestylecopy)listssc.get(i)).getZhipiaoe());
      cb.setDianhui(((Settlestylecopy)listssc.get(i)).getDianhuie());
      cb.setMingxi(((Settlestylecopy)listssc.get(i)).getMingxi());
      cb.setStbeizhu(((Settlestylecopy)listssc.get(i)).getStbeizhu());
      cb.setQiankuan(((Settlestylecopy)listssc.get(i)).getQiankuane());
      cb.setHuaka(((Settlestylecopy)listssc.get(i)).getHuakae());
      cb.setYajintrans(((Settlestylecopy)listssc.get(i)).getYajintranse());
      cb.setFuka(((Settlestylecopy)listssc.get(i)).getFukae());
      cb.setAilibang(((Settlestylecopy)listssc.get(i)).getAilibange());
      cb.setBeiyong1(((Settlestylecopy)listssc.get(i)).getMeibeiere());
      cb.setBeiyong2(((Settlestylecopy)listssc.get(i)).getNuomeiere());
      cb.setYimei(((Settlestylecopy)listssc.get(i)).getYimeie());
      cb.setDongfang(((Settlestylecopy)listssc.get(i)).getGonge());
      cb.setGengmei(((Settlestylecopy)listssc.get(i)).getGengmeie());
      cb.setXinyang(((Settlestylecopy)listssc.get(i)).getXinyange());
      cb.setYuemei(((Settlestylecopy)listssc.get(i)).getYuemeie());
      cb.setZhenyoumei(((Settlestylecopy)listssc.get(i)).getZhenyoumeie());
      cb.setBankadate(((BillMainPub)listbmp.get(i)).getBankadate());
      cb.setYuyuedate(((BillMainPub)listbmp.get(i)).getYueyedate());
      cb.setFapiaohao(((BillMainPub)listbmp.get(i)).getFapiaohao());
      cb.setBeizhu(((BillSub)listbs.get(i)).getRemark());
      cb.setChukudan(((BillSub)listbs.get(i)).getChuku());
      cb.setShoukuanren(((BillMainPub)listbmp.get(i)).getShoukuanren());
      cb.setOtherpay(md+"");
      cb.setOtherrecive(mc+"");
      cb.setLsh((String)listlsh.get(i));
      cb.setYscrop(((Ysyf)ylist.get(i)).getYscrop());
      cb.setYfcrop(((Ysyf)ylist.get(i)).getYfcrop());
      cb.setBeiyong(((Settlestylecopy)listssc.get(i)).getBeiyonge());
      cb.setCropid(cbs.getCropid());
      cb.setJiesuanbz(listsst.get(i));
      listcb.add(cb);
    }

    this.cbm.deleteall();

    for (int i = 0; i < listcb.size(); i++) {
      this.cbm.insertcopy((ComBill)listcb.get(i));
    }
    String maxlsh = this.cbm.getMaxLsh(cbs.getCropid());
    Cobp cbp = new Cobp();
    cbp.setCropid(cbs.getCropid());
    cbp.setLsh(maxlsh);

    if (maxlsh == null) {
      for (int i = 0; i < listcb.size(); i++)
      {
        this.cbm.insert((ComBill)listcb.get(i));
      }

      this.cbm.deleteall();
    } else {

    	List<ComBill> listcbnew = this.cbm.selectNew(cbp);

      if ((listcbnew != null) && (listcbnew.size() != 0))
      {
        for (int i = 0; i < listcbnew.size(); i++)
        {
          this.cbm.insert((ComBill)listcbnew.get(i));
          this.cbm.deleteall();
        }
        
   	 List<Integer> slist =  cbm.selectId(cbs.getCropid());
	   	for(int i =0;i<listcb.size();i++){
	   		listcb.get(i).setId(slist.get(i));
	   		int o =cbm.updateByPrimaryKey(listcb.get(i));
	   	}	
      }else{
    		 List<Integer> slist =  cbm.selectId(cbs.getCropid());
    		   	for(int i =0;i<listcb.size();i++){
    		   		listcb.get(i).setId(slist.get(i));
    		   		int o =cbm.updateByPrimaryKey(listcb.get(i));
    		   	}
      }

    }

    List lsh = new ArrayList();
    List slsh = new ArrayList();
    List<ComBill> afterc = this.cbm.selectbyCB2(cbs);
    
    List mp = new ArrayList();
    List r = new ArrayList();
    int temp = 0;
    for (int i = 0; i < afterc.size(); i++) {
      lsh.add(((ComBill)afterc.get(i)).getLsh());
    }
    slsh = ShuZuUtil.quChong(lsh);
    for (int i = 0; i < slsh.size(); i++) {
     // List listb = this.billSubMapper.getByLsh((String)slsh.get(i));
    	List listb = this.cbm.getByLsh((String)slsh.get(i));
    	r.add(Integer.valueOf(listb.size()));
    }
    for (int i = 0; i < r.size(); i++) {
      MergPram m = new MergPram();
      if (i == 0) {
        m.setIndex("0");
        m.setSize(r.get(i)+"");
      } else {
        for (int j = 0; j < i; j++) {
           temp += ((Integer)r.get(j)).intValue();
        }
        m.setIndex(temp+"");
        m.setSize(r.get(i)+"");
        temp = 0;
      }
      mp.add(m);
    }
    map.clear();
    map.put("page", Integer.valueOf(cbs.getPagenum()));
//    map.put("total", listcb.size());
    map1.put("rows", afterc);
    map1.put("sizes", mp);
    map1.put("lsh", slsh);
    map.put("data", map1);
    return map;
  }








public Object creteExcel(HttpServletRequest request, Excel ex)
{
  Map map = new HashMap();
  Map data = new HashMap();
  map.clear();
  String[] r = ex.getParams().split(",");
  List temp = new ArrayList();
  List params = new ArrayList();
  List lblsh = new ArrayList();
  for (int i = 0; i < r.length; i++) {
    temp.add(r[i]);
  }
  params = ShuZuUtil.quChong(temp);
  for (int i = 0; i < params.size(); i++) {
    Cobp cbp = new Cobp();
    cbp.setCropid(ex.getCropid());
    cbp.setLsh((String)params.get(i));
    List<ComBill> cblbylsh = this.cbm.selectByLsh(cbp);

    for (int j = 0; j < cblbylsh.size(); j++) {
      if (cblbylsh.size() == 1) {
        lblsh.add((ComBill)cblbylsh.get(j));
      }
      else if (j == 0) {
        lblsh.add((ComBill)cblbylsh.get(j));
      } else {
        ((ComBill)cblbylsh.get(j)).setCash("");
        ((ComBill)cblbylsh.get(j)).setShuaka("");
        ((ComBill)cblbylsh.get(j)).setZhipiao("");
        ((ComBill)cblbylsh.get(j)).setDianhui("");
        ((ComBill)cblbylsh.get(j)).setQiankuan("");
        ((ComBill)cblbylsh.get(j)).setHuaka("");
        ((ComBill)cblbylsh.get(j)).setYajintrans("");

        ((ComBill)cblbylsh.get(j)).setBeiyong1("");
        ((ComBill)cblbylsh.get(j)).setBeiyong2("");
        ((ComBill)cblbylsh.get(j)).setYimei("");
        ((ComBill)cblbylsh.get(j)).setDongfang("");
        ((ComBill)cblbylsh.get(j)).setAilibang("");
        ((ComBill)cblbylsh.get(j)).setGengmei("");
        ((ComBill)cblbylsh.get(j)).setXinyang("");
        ((ComBill)cblbylsh.get(j)).setYuemei("");
        ((ComBill)cblbylsh.get(j)).setZhenyoumei("");

        ((ComBill)cblbylsh.get(j)).setFuka("");
        ((ComBill)cblbylsh.get(j)).setOtherpay("");
        ((ComBill)cblbylsh.get(j)).setOtherrecive("");

        ((ComBill)cblbylsh.get(j)).setYscrop("");
        ((ComBill)cblbylsh.get(j)).setYfcrop("");
        ((ComBill)cblbylsh.get(j)).setJiesuanbz("");
        lblsh.add((ComBill)cblbylsh.get(j));
      }
    }

  }

  String[] lies = { "日期", "手术医师", "配台医师", "非手术医师", "顾客姓名", "现场咨询", "网络咨询", "电话咨询", "市场部", "门诊", "其他", "会员名称", 
    "消费大类", "项目分类", "消费明细", "应缴费金额","折扣率", "优惠金额", "实际消费金额","现金", "POS刷卡", "支票", "电汇", "欠款", "划卡", "押金转入", 
    "美尔贝", "诺美尔", "依美网", "东方虹", " 爱立邦", "更美", "新氧", "悦美", "真优美", "福卡", "服务项目", "其他付款", "应付公司", "其他应收", 
    "应收公司", "预约金时间", "办卡时间", "发票号", "备注", "出库单", "收款人", "非手术备注","明细","结算备注","医师助手","结算明细" };

  HSSFWorkbook workbook = new HSSFWorkbook();

  HSSFSheet sheet = workbook.createSheet();

  HSSFRow row = sheet.createRow(0);
  for (short i = 0; i < lies.length; i = (short)(i + 1)) {
    HSSFCell cell = row.createCell(i);
    HSSFRichTextString text = new HSSFRichTextString(lies[i]);
    cell.setCellValue(text);
  }

  List rows = new ArrayList();
  Iterator it = lblsh.iterator();
  int index = 0;
  while (it.hasNext()) {
    index++;

    row = sheet.createRow(index);
    ComBill cb = (ComBill)it.next();

    HSSFCell cell0 = row.createCell((short)0);
    HSSFCell cell1 = row.createCell((short)1);
    HSSFCell cell2 = row.createCell((short)2);
    HSSFCell cell3 = row.createCell((short)3);
    HSSFCell cell4 = row.createCell((short)4);
    HSSFCell cell5 = row.createCell((short)5);
    HSSFCell cell6 = row.createCell((short)6);
    HSSFCell cell7 = row.createCell((short)7);
    HSSFCell cell8 = row.createCell((short)8);
    HSSFCell cell9 = row.createCell((short)9);
    HSSFCell cell10 = row.createCell((short)10);
    HSSFCell cell11 = row.createCell((short)11);
    HSSFCell cell12 = row.createCell((short)12);
    HSSFCell cell13 = row.createCell((short)13);
    HSSFCell cell14 = row.createCell((short)14);
    HSSFCell cell15 = row.createCell((short)15);
    HSSFCell cell16 = row.createCell((short)16);
    HSSFCell cell17 = row.createCell((short)17);
    HSSFCell cell18 = row.createCell((short)18);
    HSSFCell cell19 = row.createCell((short)19);
    HSSFCell cell20 = row.createCell((short)20);
    HSSFCell cell21 = row.createCell((short)21);
    HSSFCell cell22 = row.createCell((short)22);
    HSSFCell cell23 = row.createCell((short)23);
    HSSFCell cell24 = row.createCell((short)24);
    HSSFCell cell25 = row.createCell((short)25);
    HSSFCell cell26 = row.createCell((short)26);
    HSSFCell cell27 = row.createCell((short)27);
    HSSFCell cell28 = row.createCell((short)28);
    HSSFCell cell29 = row.createCell((short)29);
    HSSFCell cell30 = row.createCell((short)30);
    HSSFCell cell31 = row.createCell((short)31);
    HSSFCell cell32 = row.createCell((short)32);
    HSSFCell cell33 = row.createCell((short)33);
    HSSFCell cell34 = row.createCell((short)34);
    HSSFCell cell35 = row.createCell((short)35);
    HSSFCell cell36 = row.createCell((short)36);
    HSSFCell cell37 = row.createCell((short)37);
    HSSFCell cell38 = row.createCell((short)38);
    HSSFCell cell39 = row.createCell((short)39);
    HSSFCell cell40 = row.createCell((short)40);
    HSSFCell cell41 = row.createCell((short)41);
    HSSFCell cell42 = row.createCell((short)42);
    HSSFCell cell43 = row.createCell((short)43);
    HSSFCell cell44 = row.createCell((short)44);
    HSSFCell cell45 = row.createCell((short)45);
    HSSFCell cell46 = row.createCell((short)46);
    HSSFCell cell47 = row.createCell((short)47);
    
    HSSFCell cell48 = row.createCell((short)48);
    HSSFCell cell49 = row.createCell((short)49);
    HSSFCell cell50 = row.createCell((short)50);
    HSSFCell cell51 = row.createCell((short)51);
    
    
    HSSFRichTextString date = new HSSFRichTextString(cb.getDate());
    HSSFRichTextString shoushu = new HSSFRichTextString(cb.getShoushudoc());
    HSSFRichTextString peitai = new HSSFRichTextString(cb.getPeitai());
    HSSFRichTextString feishoushu = new HSSFRichTextString(cb.getFeishoushu());
    HSSFRichTextString cusname = new HSSFRichTextString(cb.getCusname());
    HSSFRichTextString xianchang = new HSSFRichTextString(cb.getXczx());

    HSSFRichTextString net = new HSSFRichTextString(cb.getWebzx());
    HSSFRichTextString phone = new HSSFRichTextString(cb.getPhonezx());
    HSSFRichTextString scb = new HSSFRichTextString(cb.getScb());
    HSSFRichTextString menzhen = new HSSFRichTextString(cb.getMenzhen());
    HSSFRichTextString qita = new HSSFRichTextString(cb.getQita());

    HSSFRichTextString huiyuandanwei = new HSSFRichTextString(cb.getMembername());
    HSSFRichTextString xfdl = new HSSFRichTextString(cb.getXfdl());
    HSSFRichTextString xmfl = new HSSFRichTextString(cb.getXmfl());
    HSSFRichTextString xfmx = new HSSFRichTextString(cb.getXfmx());
//cb.getSjje()
    HSSFRichTextString yjine = new HSSFRichTextString(cb.getSjje());

    HSSFRichTextString zhelv = new HSSFRichTextString(cb.getZhekopulv());
    HSSFRichTextString youhui = new HSSFRichTextString(cb.getYouhuijine());

    HSSFRichTextString sjje = new HSSFRichTextString(cb.getJfje());
    HSSFRichTextString cash = new HSSFRichTextString(cb.getCash());
    HSSFRichTextString pos = new HSSFRichTextString(cb.getShuaka());
    HSSFRichTextString zhipiao = new HSSFRichTextString(cb.getZhipiao());
    HSSFRichTextString dianhui = new HSSFRichTextString(cb.getDianhui());
    HSSFRichTextString qiankuan = new HSSFRichTextString(cb.getQiankuan());
    HSSFRichTextString huaka = new HSSFRichTextString(cb.getHuaka());
    HSSFRichTextString yajin = new HSSFRichTextString(cb.getYajintrans());

    HSSFRichTextString meierbei = new HSSFRichTextString(cb.getBeiyong1());
    HSSFRichTextString nuomeier = new HSSFRichTextString(cb.getBeiyong2());
    HSSFRichTextString yimei = new HSSFRichTextString(cb.getYimei());
    HSSFRichTextString dongfang = new HSSFRichTextString(cb.getDongfang());
    HSSFRichTextString aili = new HSSFRichTextString(cb.getAilibang());
    HSSFRichTextString gengmei = new HSSFRichTextString(cb.getGengmei());
    HSSFRichTextString xinyang = new HSSFRichTextString(cb.getXinyang());
    HSSFRichTextString yuemei = new HSSFRichTextString(cb.getYuemei());
    HSSFRichTextString zhenyoumei = new HSSFRichTextString(cb.getZhenyoumei());

    HSSFRichTextString fuka = new HSSFRichTextString(cb.getFuka());
    HSSFRichTextString fuwu = new HSSFRichTextString(cb.getBeiyong());
    HSSFRichTextString qitafu = new HSSFRichTextString(cb.getOtherpay());
    HSSFRichTextString yingfugongsi = new HSSFRichTextString(cb.getYfcrop());
    HSSFRichTextString qitashou = new HSSFRichTextString(cb.getOtherrecive());
    HSSFRichTextString yingshougongsi = new HSSFRichTextString(cb.getYscrop());
    HSSFRichTextString yuyuedate = new HSSFRichTextString(cb.getYuyuedate());
    HSSFRichTextString banka = new HSSFRichTextString(cb.getBankadate());
    HSSFRichTextString fapiaohao = new HSSFRichTextString(cb.getFapiaohao());
    HSSFRichTextString beizhu = new HSSFRichTextString(cb.getBeizhu());
    HSSFRichTextString chukudan = new HSSFRichTextString(cb.getChukudan());
    HSSFRichTextString shoukuan = new HSSFRichTextString(cb.getShoukuanren());
    HSSFRichTextString fssbz = new HSSFRichTextString(cb.getFssbz());

    HSSFRichTextString mingxi = new HSSFRichTextString(cb.getMingxi());
    HSSFRichTextString stbeizhu =  new HSSFRichTextString(cb.getStbeizhu());
    HSSFRichTextString ysassit =  new HSSFRichTextString(cb.getYsassit());
    HSSFRichTextString jiesuanbz =  new HSSFRichTextString(cb.getJiesuanbz());
    
    cell0.setCellValue(date);
    cell1.setCellValue(shoushu);
    cell2.setCellValue(peitai);
    cell3.setCellValue(feishoushu);
    cell4.setCellValue(cusname);
    cell5.setCellValue(xianchang);
    cell6.setCellValue(net);
    cell7.setCellValue(phone);
    cell8.setCellValue(scb);
    cell9.setCellValue(menzhen);
    cell10.setCellValue(qita);
    cell11.setCellValue(huiyuandanwei);
    cell12.setCellValue(xfdl);
    cell13.setCellValue(xmfl);
    cell14.setCellValue(xfmx);
    cell15.setCellValue(yjine);
    cell16.setCellValue(zhelv);
    cell17.setCellValue(youhui);
    cell18.setCellValue(sjje);
    cell19.setCellValue(cash);
    cell20.setCellValue(pos);
    cell21.setCellValue(zhipiao);
    cell22.setCellValue(dianhui);
    cell23.setCellValue(qiankuan);
    cell24.setCellValue(huaka);
    cell25.setCellValue(yajin);
    cell26.setCellValue(meierbei);
    cell27.setCellValue(nuomeier);

    cell28.setCellValue(yimei);
    cell29.setCellValue(dongfang);
    cell30.setCellValue(aili);
    cell31.setCellValue(gengmei);
    cell32.setCellValue(xinyang);
    cell33.setCellValue(yuemei);
    cell34.setCellValue(zhenyoumei);
    cell35.setCellValue(fuka);
    cell36.setCellValue(fuwu);
    cell37.setCellValue(qitafu);
    cell38.setCellValue(yingfugongsi);
    cell39.setCellValue(qitashou);
    cell40.setCellValue(yingshougongsi);
    cell41.setCellValue(yuyuedate);
    cell42.setCellValue(banka);
    cell43.setCellValue(fapiaohao);
    cell44.setCellValue(beizhu);
    cell45.setCellValue(chukudan);
    cell46.setCellValue(shoukuan);

    cell47.setCellValue(fssbz);
    cell48.setCellValue(mingxi);
    cell49.setCellValue(stbeizhu);
    cell50.setCellValue(ysassit);
    cell51.setCellValue(jiesuanbz);
    rows.add(row);
  }

  String date = TransformerStr.getStringDate();
  try
  {
    String dir = "D:\\台账记录\\";
    File file0 = new File(dir);
    file0.deleteOnExit();
    file0.mkdir();

    File file = new File("D:\\台账记录\\" + date + "台账记录.xls");
    OutputStream out = new FileOutputStream(file);
    workbook.write(out);
    if (file.isFile()) {
      file.deleteOnExit();
      file = new File(file.getAbsolutePath());
      out.close();
      file.exists();
    }
  } catch (IOException e) {
    data.put("status", Integer.valueOf(1));
    return map;
  }

  data.put("status", Integer.valueOf(0));
  map.put("data", data);

  return map;
}

}
